package com.mayuanfei.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

import java.net.URI;

/**
 * TODO 类描述
 *
 * @author 老马
 * @date 2024-12-23 14:38
 */
@Configuration
@RequiredArgsConstructor
public class S3Config {

    private final S3Properties s3Properties;


    @Bean
    public S3Client s3Client() {
        // 使用 AWS SDK 2.x 来创建 S3 客户端，连接到 Minio
        return S3Client.builder()
                // 设置 Minio 的端点
                .endpointOverride(URI.create(s3Properties.getUrl()))
                // Minio 本身不关心区域，可以选择任意区域
                .region(Region.US_EAST_1)
                .credentialsProvider(() ->
                        AwsBasicCredentials.create(s3Properties.getAccessKey()
                                , s3Properties.getSecretKey()))
                .build();
    }

    @Bean
    public S3Presigner s3Presigner() {
        return S3Presigner.builder()
                .endpointOverride(URI.create(s3Properties.getUrl()))
                .region(Region.US_EAST_1) // 区域
                .credentialsProvider(() ->
                        AwsBasicCredentials.create(s3Properties.getAccessKey()
                                , s3Properties.getSecretKey()))
                .build();
    }
}